package com.aizhixin.lab.post.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.DeleteIdDomain;
import com.aizhixin.lab.post.data.PostData;
import com.aizhixin.lab.post.domain.PostDomain;
import com.aizhixin.lab.post.domain.PostSkillDomain;
import com.aizhixin.lab.post.domain.QueryPostDomain;
import com.aizhixin.lab.post.entity.Transferability;
import com.aizhixin.lab.post.repository.TransferabilityRepository;
import com.aizhixin.lab.post.server.PostService;
import com.aizhixin.lab.project.template.domain.PostInfoDomain;
import com.aizhixin.lab.project.template.domain.ProjectCourseDomain;
import com.aizhixin.lab.project.template.domain.ProjectDomain;
import com.aizhixin.lab.project.template.domain.QueryProjectDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/post")
@Api(value = "岗位管理", description = "新增,删除岗位")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private TransferabilityRepository transferabilityRepository;
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存/编辑", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody PostDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            postService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);

    }
    @RequestMapping(value = "/findPost", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "岗位首页查询", httpMethod = "POST", notes = "岗位首页查询<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = PostDomain.class)})
    public ResponseEntity<?> findProject(@RequestHeader("Authorization") String token, @RequestBody QueryPostDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            PageData<PostDomain> post = postService.findPost(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,post);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findPostInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取岗位详情", httpMethod = "GET", notes = "获取岗位详情<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = PostData.class)})
    public ResponseEntity<?> findPostInfo(@RequestHeader("Authorization") String token,@RequestParam String id){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            PostData postInfo = postService.findPostInfo(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,postInfo);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findTransferability", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取岗位下所有可迁移能力", httpMethod = "GET", notes = "获取岗位下所有可迁移能力<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Transferability.class)})
    public ResponseEntity<?> findTransferability(@RequestHeader("Authorization") String token,@RequestParam String id){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(id)){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,"岗位id不能为空");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        try{
            List<Transferability> transferabilityList = transferabilityRepository.findByPostIdOrderByNoAsc(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,transferabilityList);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/deletePost", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除岗位", httpMethod = "DELETE", notes = "删除岗位<br><br><b>lwq</b>")
    public ResponseEntity<?> deletePost(@RequestHeader("Authorization") String token, @RequestParam(value = "id") String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            postService.deletePost(id);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }

        return   new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findProject", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取岗位相关所有项目名称", httpMethod = "GET", notes = "获取岗位相关所有项目<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectDomain.class)})
    public ResponseEntity<?> findProject(@RequestHeader("Authorization") String token,@RequestParam String id){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<ProjectDomain> project = postService.findProjectByPostId(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,project);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findCourse", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取岗位相关所有课程名称", httpMethod = "GET", notes = "根据岗位ID相关获取岗位相关所有课程<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ProjectCourseDomain.class)})
    public ResponseEntity<?> findCourse(@RequestHeader("Authorization") String token,@RequestParam String id){
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<ProjectCourseDomain> courseDomainList = postService.findCourseByPostId(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,courseDomainList);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

}
